package com.tg.app.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.demo.view.TGBottomSheetDialog;
import com.tg.app.R;
import com.tg.appcommon.android.ResourcesUtil;
import com.tange.base.toolkit.StringUtils;
import com.tg.data.http.entity.DeviceBellBean;

import java.util.List;

public class BottomSheetListDialog  extends TGBottomSheetDialog {
    public final static int ACTION_SETTINGS = 1;
    public final static int ACTION_PRE_POSITION = 2;
    public final static int ACTION_DIALOG_PTZ = 8;
    public final static int ACTION_SPEED_X = 3;
    public final static int ACTION_SPEED_2X = 4;
    public final static int ACTION_SPEED_4X = 5;
    public final static int ACTION_SPEED_8X = 6;
    public final static int ACTION_RESOLUTION = 7;
    public final static int ACTION_MODE_STANDAR = 8;
    public final static int ACTION_MODE_CUSTOMIZE = 9;

    public final static int ACTION_MODE_20 = 10;
    public final static int ACTION_MODE_40 = 11;
    public final static int ACTION_MODE_60 = 12;
    public final static int ACTION_MODE_ALL = 13;


    private  BottomSheetAdapter adapter;
    private BottomSheetClickListener listener;


    public BottomSheetListDialog(@NonNull Context context) {
        super(context);
    }

    public BottomSheetListDialog(@NonNull Context context, String from) {
        super(context, from);
    }

    public void setClickListener(@NonNull BottomSheetClickListener listener) {
        this.listener = listener;
    }


    @Override
    protected int getContentView() {
        if (StringUtils.equalsIgnoreCase(FROM_LAMP_WIFI_VIDEO, from)){
            return R.layout.layout_lamp_bottom_sheet;
        }
        return R.layout.layout_bottom_sheet;
    }

    @SuppressLint("ResourceAsColor")
    @Override
    protected void initView(@NonNull View view) {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        RecyclerView recyclerView = view.findViewById(R.id.bottom_sheet);
        recyclerView.setLayoutManager(linearLayoutManager);
        adapter = new BottomSheetAdapter();



        adapter.setFrom(this.from);
        recyclerView.setAdapter(adapter);

        bottomSheetDialog.getWindow().findViewById(R.id.design_bottom_sheet).setBackgroundResource(android.R.color.transparent);

        TextView textView = view.findViewById(R.id.bottom_sheet_exit);
        if (StringUtils.equals(this.from, TGBottomSheetDialog.FROM_SOLAR_VIDEO_MODE)){
            textView.setTextColor(ResourcesUtil.getResources().getColor(R.color.car_4g_device_add_title_color));
        }
        if (textView != null){
            textView.setVisibility(View.GONE);
            textView.setOnClickListener(v -> dismiss());
        }

    }

    @SuppressLint("NotifyDataSetChanged")
    public void setData(@NonNull List<BottomSheetItem> bottomSheetItem) {
        adapter.setBottomSheetItem(bottomSheetItem);
        adapter.notifyDataSetChanged();
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setLampData(@NonNull List<DeviceBellBean> bottomSheetItem) {

        adapter.notifyDataSetChanged();
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setSelectedIndex(int index) {
        adapter.setSelectedIndex(index);
        adapter.notifyDataSetChanged();
    }

    public void setNoneSelected() {
        setSelectedIndex(-1);
    }

    public static class BottomSheetItem {
        public int action;
        public int iconRes;
        @Nullable
        public String text;
        public String mark;
        public boolean isCheck = false;
        public boolean isShowCheckBox = false;

        public BottomSheetItem(){}
        public BottomSheetItem(int action, int iconRes, @Nullable String text, boolean isShowCheckBox, boolean isCheck){
            this.action = action;
            this.iconRes = iconRes;
            this.text = text;
            this.isCheck = isCheck;
            this.isShowCheckBox = isShowCheckBox;
        }

        public BottomSheetItem(int action, int iconRes, @Nullable String text){
            this.action = action;
            this.iconRes = iconRes;
            this.text = text;
        }

        public BottomSheetItem(int action, int iconRes, @Nullable String text, String mark, boolean isCheck){
            this.action = action;
            this.iconRes = iconRes;
            this.text = text;
            this.mark = mark;
            this.isShowCheckBox = true;
            this.isCheck = isCheck;
        }

    }

    class BottomSheetAdapter extends RecyclerView.Adapter<BottomSheetViewHolder> {
        private List<BottomSheetItem> bottomSheetItems;
        private int mSelectedIndex = -1;
        private Context mContext;
        private String mFrom;

        public void setFrom(String from){
            this.mFrom = from;
        }

        public void setSelectedIndex(int selectedIndex) {
            this.mSelectedIndex = selectedIndex;
        }

        public void setBottomSheetItem(List<BottomSheetItem> bottomSheetItem) {
            this.bottomSheetItems = bottomSheetItem;
        }

        @NonNull
        @Override
        public BottomSheetViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            mContext = parent.getContext();
            if (StringUtils.equals(mFrom, TGBottomSheetDialog.FROM_SOLAR_VIDEO_MODE)){
                return new BottomSheetViewHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_bottom_sheet_solar_list_item, parent, false));
            }

            if (StringUtils.equalsIgnoreCase(mFrom, TGBottomSheetDialog.FROM_LAMP_WIFI_VIDEO)){
                return new BottomSheetViewHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_lamp_bottom_sheet_list_item, parent, false));
            }

            return new BottomSheetViewHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_bottom_sheet_list_item, parent, false));
        }

        @Override
        public void onBindViewHolder(@NonNull BottomSheetViewHolder holder, int position) {
            BottomSheetItem item = bottomSheetItems.get(position);

            holder.textView.setText(item.text);
            if (mSelectedIndex == position) {
                holder.textView.setTextColor(mContext.getResources().getColor(R.color.global_text_color));
            } else {
                holder.textView.setTextColor(mContext.getResources().getColor(R.color.bottom_sheet_text_color));
            }
            holder.imageView.setVisibility(View.INVISIBLE);
            holder.imageViewCheck.setVisibility(View.INVISIBLE);

            holder.itemView.setOnClickListener(v -> {
                bottomSheetDialog.dismiss();
                if (listener != null) {
                    assert item.text != null;
                    listener.onClick(item.action, item.text, position);
                }
            });
        }

        @Override
        public int getItemCount() {
            return bottomSheetItems == null ? 0 : bottomSheetItems.size();
        }
    }


    public interface BottomSheetClickListener {
        void onClick(int action, @NonNull String text, int position);
    }


    public static class BottomSheetViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView textView;
        TextView textMark;
        public ImageView imageViewCheck;

        public BottomSheetViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.bottom_sheet_title);
            imageView = itemView.findViewById(R.id.bottom_sheet_icon);
            imageViewCheck = itemView.findViewById(R.id.sw_settings_content);
            textMark = itemView.findViewById(R.id.bottom_sheet_mark);
        }
    }
}
